package com.pxst.internalReports.resp;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.pxst.bank.entity.SysBankCardEntity;
import com.pxst.bank.mapper.SysBankCardMapper;
import com.pxst.constants.TransactionConstant;
import com.pxst.fi.entity.SysLogLastPageEntity;
import com.pxst.fi.mapper.SysLogLastPageMapper;
import io.swagger.annotations.ApiModel;
import lombok.Setter;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.ArrayList;

/**
 * @author Txd
 * @since 2023-09-22 14:44:41
 */
@Setter
@ApiModel(description = "返回前端的内部转账列表的查询数据")
public class InternalTransferListResp {

    @Resource
    private SysBankCardMapper sysBankCardMapper;

    @Resource
    private SysLogLastPageMapper sysLogLastPageMapper;


    /**
     * 订单完成时间
     */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date completedAt;

    private boolean retryBtn;

    private boolean updateBtn;

    private String fromAccountCode;

    private String toAccountCode;

    private String fromMobileName;

    private boolean isErrurl;

    private boolean isRunMon;


    /**
     * 内转ID
     */
    private Long trId;

    /**
     * "type 转移类型
        1 FI to FO
        2 FI to FX
        3 FI to TR
        4 TR to FO
        5 TR to FX""
     BS200"
     */
    private Integer type;

    /**
     * 银行卡ID (from)
     */
    private Long fromBankCardId;

    /**
     * 银行卡ID (to)
     */
    private Long toBankCardId;

    /**
     * 用户发起金额 (Amount)
     */
    private BigDecimal requestAmount;

    /**
     * 实际作用金额
     */
    private BigDecimal paidAmount;

    /**
     * 银行手续费
     */
    private BigDecimal bankFee;

    /**
     * "讯银回传状态
        1.Pending–等待机器人跑
         2.Processing–机器人正在跑
        3.Completed–成功交易
        4.Manual process–失败后的手动操作
        人工可操作成成功或失败（如果交易试成功的）
        5.Failed–失败, 不可更改状态
     BS200"
     */
    private Integer status;

    /**
     * 讯银步骤
     */
    private String step;

    /**
     * 创建时间
     */
    private Date createdAt;

    /**
     * 更新时间
     */
    private Date updatedAt;

    /**
     * 创建人员
     */
    private String creator;

    /**
     * 更新人员
     */
    private String updater;

    /**
     * 迅银回传当前状态（文字）
     */
    private String message;

    /**
     * 执行次数
     */
    private Integer retryTimes;

    /**
     * 出款虚拟账单ID
     */
    private Long fromVsId;

    /**
     * 收款虚拟账单ID
     */
    private Long toVsId;

    /**
     * 我们的单号
     */
    private String commandId;

    /**
     * 备注
     */
    private String note;

    /**
     * 当地更新时间
     */
    private Date localUpdatedAt;

    /**
     * 是否手动添加  1.是
     */
    private Integer isEntry;

    /**
     * VND OTP
     */
    private String vndOtp;

    /**
     * 后记
     */
    private String postscript;

    /**
     * VND 出款方式
     */
    private String vndPaymentMethod;

    /**
     * 币别
     */
    private String currency;

    /**
     * TR 自动转帐
     */
    private boolean trAuto;


    /**
     * 订单号（通过AltIdUtils工具类生成的【pxst_common.com.pxst.utils.AltIdUtils】）
     */
    private String altId;

    /**
     * "手动操作状态
     成功 Uploaded
     失败 Unknown Error After Otp
     BS200"
     */
    private Integer action;

    @JsonIgnore
    private Integer total;

    public Date getCompletedAt() {
        return this.status == TransactionConstant.STATUS_COMPLETED ? this.completedAt : null;
    }

    public boolean isRetryBtn() {
        if (this.isEntry == 1){
            return false;
        }
        return this.status == 4 && this.retryTimes < 2
                && this.action == null;
    }

    public boolean isUpdateBtn() {
        return (this.isEntry == 0 && this.status == TransactionConstant.STATUS_MANUAL_PROCESS)
                || (this.isEntry == 1 && this.status == TransactionConstant.STATUS_PENDING);
    }

    public String getFromAccountCode() {
        SysBankCardEntity systemBankCard = sysBankCardMapper.selectOne(new LambdaQueryWrapper<SysBankCardEntity>().eq(SysBankCardEntity::getId, this.fromBankCardId));
        return systemBankCard.getAccountCode();
    }

    public String getToAccountCode() {
        SysBankCardEntity systemBankCard = sysBankCardMapper.selectOne(new LambdaQueryWrapper<SysBankCardEntity>().eq(SysBankCardEntity::getId, this.toAccountCode));
        return systemBankCard.getAccountCode();
    }

    public String getFromMobileName() {
        SysBankCardEntity systemBankCard = sysBankCardMapper.selectOne(new LambdaQueryWrapper<SysBankCardEntity>().eq(SysBankCardEntity::getId, this.fromBankCardId));
        return systemBankCard.getMobileName();
    }

    public boolean isErrurl() {
        SysLogLastPageEntity systemLogLastPage = sysLogLastPageMapper.selectOne(new LambdaQueryWrapper<SysLogLastPageEntity>().eq(SysLogLastPageEntity::getCommandId, this.commandId));
        return systemLogLastPage != null;
    }

    public boolean isRunMon() throws ParseException {
        if (this.isEntry == 1) {
            return false;
        }

        ArrayList<Integer> integers = new ArrayList<>();
        integers.add(TransactionConstant.STATUS_PENDING);
        integers.add(TransactionConstant.STATUS_PROCESSING);

        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return integers.contains(this.status) &&
                (df.parse(df.format(this.updatedAt)).getTime() - df.parse(df.format(LocalDate.now())).getTime()) / 1000 >= 900;
    }

    public SysBankCardMapper getSysBankCardMapper() {
        return sysBankCardMapper;
    }

    public SysLogLastPageMapper getSysLogLastPageMapper() {
        return sysLogLastPageMapper;
    }

    public Long getTrId() {
        return trId;
    }

    public Integer getType() {
        return type;
    }

    public Long getFromBankCardId() {
        return fromBankCardId;
    }

    public Long getToBankCardId() {
        return toBankCardId;
    }

    public BigDecimal getRequestAmount() {
        return requestAmount;
    }

    public BigDecimal getPaidAmount() {
        return paidAmount;
    }

    public BigDecimal getBankFee() {
        return bankFee;
    }

    public Integer getStatus() {
        return status;
    }

    public String getStep() {
        return step;
    }

    public Date getCreatedAt() {
        return createdAt;
    }

    public Date getUpdatedAt() {
        return updatedAt;
    }

    public String getCreator() {
        return creator;
    }

    public String getUpdater() {
        return updater;
    }

    public String getMessage() {
        return message;
    }

    public Integer getRetryTimes() {
        return retryTimes;
    }

    public Long getFromVsId() {
        return fromVsId;
    }

    public Long getToVsId() {
        return toVsId;
    }

    public String getCommandId() {
        return commandId;
    }

    public String getNote() {
        return note;
    }

    public Date getLocalUpdatedAt() {
        return localUpdatedAt;
    }

    public Integer getIsEntry() {
        return isEntry;
    }

    public String getVndOtp() {
        return vndOtp;
    }

    public String getPostscript() {
        return postscript;
    }

    public String getVndPaymentMethod() {
        return vndPaymentMethod;
    }

    public String getCurrency() {
        return currency;
    }

    public boolean isTrAuto() {
        return trAuto;
    }

    public String getAltId() {
        return altId;
    }

    public Integer getAction() {
        return action;
    }

    public Integer getTotal() {
        return total;
    }

    @Override
    public String toString() {
        return "InternalTransferListResp{" +
                "sysBankCardMapper=" + sysBankCardMapper +
                ", sysLogLastPageMapper=" + sysLogLastPageMapper +
                ", completedAt=" + completedAt +
                ", retryBtn=" + retryBtn +
                ", updateBtn=" + updateBtn +
                ", fromAccountCode='" + fromAccountCode + '\'' +
                ", toAccountCode='" + toAccountCode + '\'' +
                ", fromMobileName='" + fromMobileName + '\'' +
                ", isErrurl=" + isErrurl +
                ", isRunMon=" + isRunMon +
                ", trId=" + trId +
                ", type=" + type +
                ", fromBankCardId=" + fromBankCardId +
                ", toBankCardId=" + toBankCardId +
                ", requestAmount=" + requestAmount +
                ", paidAmount=" + paidAmount +
                ", bankFee=" + bankFee +
                ", status=" + status +
                ", step='" + step + '\'' +
                ", createdAt=" + createdAt +
                ", updatedAt=" + updatedAt +
                ", creator='" + creator + '\'' +
                ", updater='" + updater + '\'' +
                ", message='" + message + '\'' +
                ", retryTimes=" + retryTimes +
                ", fromVsId=" + fromVsId +
                ", toVsId=" + toVsId +
                ", commandId='" + commandId + '\'' +
                ", note='" + note + '\'' +
                ", localUpdatedAt=" + localUpdatedAt +
                ", isEntry=" + isEntry +
                ", vndOtp='" + vndOtp + '\'' +
                ", postscript='" + postscript + '\'' +
                ", vndPaymentMethod='" + vndPaymentMethod + '\'' +
                ", currency='" + currency + '\'' +
                ", trAuto=" + trAuto +
                ", altId='" + altId + '\'' +
                ", action=" + action +
                ", total=" + total +
                '}';
    }
}
